<script setup lang="ts">
import { useVueTemplate } from "./utils/hook";
import { defineAsyncComponent, ref } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { deviceDetection } from "@pureadmin/utils";

import Delete from "~icons/ep/delete";
import EditPen from "~icons/ep/edit-pen";
import Refresh from "~icons/ep/refresh";
import AddFill from "~icons/ri/add-circle-line";
import FileIconsMicrosoftExcel from "~icons/file-icons/microsoft-excel";
import LucideBookTemplate from "~icons/lucide/book-template";
import EpUploadFilled from "~icons/ep/upload-filled";
import MaterialSymbolsCloudDownload from "~icons/material-symbols/cloud-download";

defineOptions({
  name: "permission"
});

const formRef = ref();
const tableRef = ref();
const contentRef = ref();
const {
  operateName,
  queryForm,
  loading,
  columns,
  rowStyle,
  pageList,
  pagination,
  onSearch,
  resetForm,
  openDialog,
  excelTemplate,
  beforeUpload,
  handleDelete,
  handleDeleteBatch,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  SelectionExport,
  SelectionExportRef,
  visibleExportExcelField,
  defaultExportFieldKeys,
  confirmExportExcel,
  showSelectionExport
} = useVueTemplate();

/**覆盖默认的上传行为 */
const requestUpload = () => Promise.resolve();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="queryForm"
      class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="字符串类型" prop="name">
        <el-input
          v-model="queryForm.string"
          placeholder="请输入字符串类型"
          clearable
          class="w-[180px]!"
        />
      </el-form-item>
      <el-form-item label="日期类型" prop="date">
        <el-date-picker
          v-model="queryForm.date"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          placeholder="先选择日期"
          @change="
            range => (
              (queryForm.date_ge = range[0]),
              (queryForm.date_le = range[1])
            )
          "
        />
      </el-form-item>
      <el-form-item label="布尔类型" prop="name">
        <el-select v-model="queryForm.boolType" clearable class="w-[180px]!">
          <el-option label="是" value="true" />
          <el-option label="否" value="false" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri/search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <div
      ref="contentRef"
      :class="['flex', deviceDetection() ? 'flex-wrap' : '']"
    >
      <PureTableBar
        :title="`${operateName}管理`"
        :columns="columns"
        @refresh="onSearch"
      >
        <template #buttons>
          <el-button
            type="primary"
            :icon="useRenderIcon(AddFill)"
            @click="openDialog()"
          >
            新增
          </el-button>

          <el-dropdown class="mr-2 ml-2">
            <el-button
              type="success"
              :icon="useRenderIcon(FileIconsMicrosoftExcel)"
            >
              Excel
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>
                  <el-button
                    link
                    type="primary"
                    :icon="useRenderIcon(LucideBookTemplate)"
                    @click="excelTemplate"
                  >
                    模板
                  </el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-upload
                    action="#"
                    :http-request="requestUpload"
                    :show-file-list="false"
                    :on-change="beforeUpload"
                  >
                    <el-button
                      link
                      type="primary"
                      :icon="useRenderIcon(EpUploadFilled)"
                    >
                      导入
                    </el-button>
                  </el-upload>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button
                    link
                    type="primary"
                    :icon="useRenderIcon(MaterialSymbolsCloudDownload)"
                    @click="showSelectionExport"
                  >
                    导出
                  </el-button>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>

          <el-popconfirm
            :title="`是否批量删除勾选的${operateName}?`"
            @confirm="handleDeleteBatch"
          >
            <template #reference>
              <el-button type="danger" :icon="useRenderIcon(Delete)">
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <pure-table
            ref="tableRef"
            align-whole="center"
            showOverflowTooltip
            table-layout="auto"
            :loading="loading"
            :size="size"
            adaptive
            select-on-indeterminate
            stripe
            border
            :row-style="rowStyle"
            :adaptiveConfig="{ offsetBottom: 108 }"
            :data="pageList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            :row-key="row => row.id"
            @selection-change="handleSelectionChange"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(EditPen)"
                @click="openDialog('修改', row)"
              >
                修改
              </el-button>
              <el-popconfirm
                :title="`是否确认删除这条${operateName}数据`"
                @confirm="handleDelete(row)"
              >
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="danger"
                    :size="size"
                    :icon="useRenderIcon(Delete)"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
    <SelectionExport
      ref="SelectionExportRef"
      v-model="visibleExportExcelField"
      :default-checked-keys="defaultExportFieldKeys"
      :default-export-file-name="`${operateName}数据导出表`"
      @confirm="confirmExportExcel"
    />
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.operation_bar_btn_interval {
  margin-left: 12px;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
